<template>
	<view class="container">
		<view class="content">
			<view class="photo">
				<image :src="img"></image>
			</view>
			<view class="list">
				<view class="inner b-s" v-for="(item,index) in exam" :key="item.id" @click="sign(item.id,item.title,item.startTime,item.leave,item.number,index,item.state)">
					<uni-card title="等级考试报名" thumbnail="../../static/shouye_03.png" :extra="(item.number)/100+'元'" :note="item.leave+'级'">
					   <p>{{item.title}}</p>
					   <p>报名截止时间：{{item.upToDate | dateModule}}</p>
					   <p>考试开始时间：{{item.startTime | dateModule}}</p>
					   <p>考试结束时间：{{item.endTime | dateModule}}</p>
					</uni-card>				
				</view>
			</view>
		</view>
		

	</view>
	
	</view>
</template>
<script>	
	import uniCard from '@/components/uni-card/uni-card.vue'
	
	import {exam,getAllPicture,paystatus,isAnswer} from '../../apis'
	export default {
		components: {
			uniCard
		},
		data() {
			return {
				exam: [],
				img:''
			}
		},
		onPullDownRefresh() {
			this.$forceUpdate();
			        console.log('refresh');
			        exam().then((res) => {
			        	let [err, data] = res
			        	console.log(data.data.data)
			        	this.exam = data.data.data
			        }),
			        getAllPicture().then((res)=>{
			        	let [err,data] = res
			        	this.img = data.data.data[4].imageUrl;
			        	uni.hideLoading();
			        })
					setTimeout(function () {
					            uni.stopPullDownRefresh();
					        }, 1000);
			    },
		onShow() {
			console.log('进入onshow')
			uni.showLoading({
				title: '加载中'
			});
			exam().then((res) => {
				let [err, data] = res
				console.log(err)
				console.log(data)
				console.log(data.data.data)
				this.exam = data.data.data
			}),
			getAllPicture().then((res)=>{
				let [err,data] = res
				this.img = data.data.data[4].imageUrl;
				uni.hideLoading();
			})
			//  var that=this;
			// this.$nextTick(function(){
				
			// })
			uni.hideLoading();
		},
		methods: {
			refresh(){
				exam().then((res) => {
					let [err, data] = res
					console.log(data.data.data)
					this.exam = data.data.data
					console.log('刷新请求结果')
					console.log(data.data.data)
				}),
				getAllPicture().then((res)=>{
					let [err,data] = res
					this.img = data.data.data[4].imageUrl;
					uni.hideLoading();
				})
			},
			//添加支付页面，现在需要跳转去支付页面
			sign(id,title,startTime,leave,number,index,state) {
				//获取当前时间的毫秒值
				 var timestamp = (new Date()).valueOf()
				 console.log("当前时间"+timestamp)
				 console.log("考试时间"+startTime)
				 isAnswer({
					 "examId":id
				 }).then((res)=>{
					 let [err,data] = res
					 console.log(id)
					 console.log('是否参加过考试')
					 console.log(data.data.data == true)
					if(data.data.data == true){
						console.log('参加过考试，进入答题结果')
						uni.navigateTo({
							url:'../answerResult/answerResult?examId='+id
						})
					}else{
						console.log('未参加过考试，进入其他')
						
						
						//当前时间大于考试报名截止时间提示用户不能进入报名
						if((new Date()).valueOf() > this.exam[index].upToDate){
							
							//支付过，判断考试结束时间。未支付，不允许报名
							paystatus({
								"examId":id
							}).then((res)=>{
								let [err,data]=res
								console.log('是否支付过考试')
								console.log(data.data.data)
								if(data.data.data == "已支付"){
									//判断考试结束时间
									if((new Date()).valueOf() > this.exam[index].endTime){
										uni.showToast({
											title:'考试已结束',
											icon:'none',
											duration:3000
										})
									}else{
										uni.navigateTo({
											url: '../answer/answer?id='+id+'&title='+title+'&startTime='+startTime+'&leave='+leave+'&state='+state
										})
									}
									
								}else if(data.data.data == "未支付"){
									//不允许报名
									uni.showToast({
										title:'很遗憾！该场考试报名已结束，请参加下一场考试',
										icon:'none',
										duration:3000
									})
								}else{
									uni.showToast({
									    title: '支付异常，请稍后再试',
									    duration: 2000
									});
								}
							})
							
						}else{
							 //判断用户是否支付过考试，完成支付跳到答题页面，未完成支付跳到支付页面
							paystatus({
								"examId":id
							}).then((res)=>{
								let [err,data]=res
								console.log('是否支付过考试')
								console.log(data.data.data)
								if(data.data.data == "已支付"){
									uni.navigateTo({
									    url: '../answer/answer?id='+id+'&title='+title+'&startTime='+startTime+'&leave='+leave+'&state='+state
									});
								}else if(data.data.data == "未支付"){
									uni.navigateTo({
										url:'../payMent/payMent?number='+number+'&id='+id
										// +'&title='+title+'&startTime='+startTime+'&leave='+leave
									});
								}else{
									uni.showToast({
									    title: '支付异常，请稍后再试',
									    duration: 2000
									});
								}
							})
							
						}
					}
				 })
			},
			onShareAppMessage: function(e) {
							let title = '蓝天飞梦小程序'
							return {
								title: title,
								path: 'pages/index/index'
							}
						}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		width: 95%;

		.photo {
			height: 150px;
			border: 1px solid #C0C0C0;
			margin: 0 auto;
			margin-top: 10px;
			margin-bottom: 15px;
		}

		.list {
			text-align: center;
			padding-top: 10px;
			padding-bottom: 10px;

			.inner {
				border-bottom: 1px dashed #cdcccc;
				margin-top: 10px;
				background-color: #e6ffff;
			}
		}

	}
</style>
